using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using MyMES.IServices.Equipments;
using System.Threading.Tasks;
using VOL.Core.Controllers.Basic;
using VOL.Core.Filters;
using VOL.Core.Utilities.Response;
using VOL.Entity.AttributeManager;
using VOL.Entity.DomainModels.Equipment;

namespace VOL.WebApi.Controllers.MyMES
{
    /// <summary>
    /// 设备类型管理控制器
    /// </summary>
    [Route("api/EquipmentType")]
    [ApiController]
    [AllowAnonymous] // 允许匿名访问，解决可能的授权问题
    [PermissionTable(Name = "EquipmentType")]
    public class EquipmentTypeController : ApiBaseController<IEquipmentTypeService>
    {
        private readonly IEquipmentTypeService _equipmentTypeService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="equipmentTypeService">设备类型服务</param>
        public EquipmentTypeController(IEquipmentTypeService equipmentTypeService)
            : base(equipmentTypeService)
        {
            _equipmentTypeService = equipmentTypeService;
        }

        /// <summary>
        /// 获取设备类型列表
        /// </summary>
        /// <param name="typeName">设备类型名称（可选）</param>
        /// <param name="status">状态（可选）</param>
        /// <returns>设备类型列表（树形结构）</returns>
        [HttpGet("GetEquipmentTypes")]
        public async Task<ActionResult> GetEquipmentTypes([FromQuery] string? typeName, [FromQuery] int? status)
        {
            return Json(await _equipmentTypeService.GetEquipmentTypes(typeName, status));
        }

        /// <summary>
        /// 添加设备类型
        /// </summary>
        /// <param name="equipmentType">设备类型实体</param>
        /// <returns>操作结果</returns>
        [HttpPost("AddEquipmentType")]
        public async Task<WebResponseContent> AddEquipmentType([FromBody] EquipmentType equipmentType)
        {
            return await _equipmentTypeService.AddEquipmentType(equipmentType);
        }

        /// <summary>
        /// 更新设备类型
        /// </summary>
        /// <param name="equipmentType">设备类型实体</param>
        /// <returns>操作结果</returns>
        [HttpPost("UpdEquipmentType")]
        public async Task<WebResponseContent> UpdEquipmentType([FromBody] EquipmentType equipmentType)
        {
            return await _equipmentTypeService.UpdEquipmentType(equipmentType);
        }

        /// <summary>
        /// 删除设备类型
        /// </summary>
        /// <param name="ids">设备类型ID列表，以逗号分隔的字符串</param>
        /// <returns>操作结果</returns>
        [HttpPost("DelEquipmentTypes")]
        public async Task<WebResponseContent> DelEquipmentTypes([FromQuery] int id)
        {
            return await _equipmentTypeService.DelEquipmentTypes(id);
        }
    }
} 